<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>SpiderFlow</title>
	<link rel="stylesheet" href="js/layui/css/layui.css" />
	<link rel="stylesheet" href="css/layui-blue.css" />
	<script type="text/javascript" src="js/layui/layui.all.js" ></script>
	<script type="text/javascript" src="js/common.js" ></script>
</head>
<body style="padding:5px;">
<div class="layui-form-item">
	<label class="layui-form-label">爬虫名称</label>
	<div class="layui-input-inline">
		<input type="text" name="Q_name_S_LK" required  lay-verify="required" placeholder="请输入爬虫名称" autocomplete="off" class="layui-input">
	</div>
	<div class="layui-input-inline" style="margin-top:5px">
		<a class="layui-btn layui-btn-sm layui-btn-normal btn-search"><i class="layui-icon">&#xe615;</i> 搜索</a>
		<a class="layui-btn layui-btn-sm layui-btn-normal btn-add"><i class="layui-icon">&#xe654;</i> 添加流程</a>
	</div>
</div>

<hr>
<table class="layui-table" id="table" lay-filter="table"></table>
<script>
	var $ = layui.$;
	var $table = layui.table.render({
		id : 'table',
		elem : '#table',
		url : 'api/spider/flow/table',
		method : 'get',
		page : true,
        response: {
            statusName: 'code', //规定数据状态的字段名称，默认：code
            statusCode: 200, //规定成功的状态码，默认：0
            msgName: 'msg' //规定状态信息的字段名称，默认：msg
        },
		parseData : function(resp){
			return {
				code : resp.code,
				data : resp.param.data,
				count : resp.param.recordsTotal
			}
		},
		cols : [[{
			title : '序号',
			width : 35,
			type : 'numbers',
			align : 'center'
		},{
			title : '爬虫名称',
			field : 'name',
            width : 180,
			templet : function(row){
				return '<a class="layui-btn layui-btn-sm btn-details" data-name="'+row.name+'" data-id="'+row.id+'">'+row.name+'</a>';
			}
		},{
			title : 'cron',
			field : 'cron',
			width : 120,
			templet : function(row){
				return '<a class="layui-btn layui-btn-sm btn-edit-cron" data-id="'+row.id+'" data-cron="'+row.cron+'">'+(row.cron || '编辑cron')+'</a>';
			}
		},{
			title : '定时/长任务',
			field : 'enabled',
			width : 105,
			templet : function(row){
				return '<input type="checkbox" data-cron="'+(row.cron||'')+'" name="switch" lay-skin="switch" lay-text="定时|长任务" value="'+row.id+'" '+(row.enabled == 1 ? 'checked':'')+'>';
			}
		},{
			title : '创建时间',
			width : 160,
			field : 'createDate',
			align : 'center'
		},{
			title : '上次执行时间',
			width : 160,
			field : 'lastExecuteTime',
			align : 'center'
		},{
			title : '状态',
			width : 120,
			field : 'executeCount',
			align : 'center',
			templet : '#execute-count'
		},{
			title : '下次执行时间',
			width : 160,
			field : 'nextExecuteTime',
			align : 'center'
		},{
			title : '操作',
			width : 195,
			align : 'center',
			templet : '#buttons'
		}]]
	});
	layui.form.on('switch',function(e) {
		if(e.elem.checked && !$(e.elem).data('cron')) {
			layui.layer.msg('cron表达式不能为空！');
			e.elem.checked = false;
			layui.form.render();
			return;
		}
		$.ajax({
            url : 'api/spider/flow/' + (e.elem.checked ? 'start': 'stop') + '/' + e.value,
			success : function(res){
			    if (res.code == 200) {
                    layui.layer.msg((e.elem.checked ? '切换为定时任务': '切换为长任务') + '成功');
				} else {
                    layui.layer.msg((e.elem.checked ? '切换为定时任务': '切换为长任务') + '失败');
				}
			},
			error : function(){
				layui.layer.msg((e.elem.checked ? '切换为定时任务': '切换为长任务') + '失败');
			}
		});
	});
	function reloadTable(){
		$table.reload({
			where : {
                Q_name_S_LK : $('input[name=Q_name_S_LK]').val()
			},
			page : {
				curr : 1
			}
		})
	}
	$("body").on('click','.btn-search',function(){
		reloadTable();
	}).on('click','.btn-add',function(){
		var layer_index = parent.layer.open({
			type: 2,
			area: ['850px', '660px'],
			maxmin: true,
			content: sf.contentPath() + 'editor.html'
		});
		parent.layer.full(layer_index);
	}).on('click','.btn-remove',function(){
		var id = $(this).data('id');
		layui.layer.confirm('您确定要删除此爬虫吗？',{
			title : '删除'
		},function(index){
			$.ajax({
				url : 'api/spider/flow/delete/' + id,
                type : 'POST',
				success : function(res){
				    if(res.code == '200'){
                        layui.layer.msg('删除成功',{time : 500},function(){
                            $table.reload();
                        })
					}else{
                        layui.layer.msg(res.param)
					}
				},
				error : function(){
					layui.layer.msg('删除失败')
				}
			});
			layui.layer.close(index);
		})
	}).on('click','.btn-run',function(){
		var id = $(this).data('id');
		layui.layer.confirm('您确定要手动运行一次该爬虫吗？',{
			title : '运行任务'
		},function(index){
			$table.reload();
			$.ajax({
				url : 'api/spider/flow/run/' + id,
				success : function(res){
				    if(res.code == '200'){
                        layui.layer.msg('手动运行成功,后台运行中',{time : 500},function(){
                            $table.reload();
                        })
					}
				},
				error : function(){
					layui.layer.msg('手动运行失败')
				}
			});
			layui.layer.close(index);
		})
	}).on('click','.btn-task',function(){
        var layer_index = parent.layer.open({
            type: 2,
            area: ['850px', '660px'],
            maxmin: true,
            content: sf.contentPath() + 'task.html?id=' + $(this).data('id') + '&name=' + encodeURIComponent(encodeURIComponent($(this).data('name')))
		});
        parent.layer.full(layer_index);
	}).on('click','.btn-log',function(){
        var layer_index = parent.layer.open({
            type: 2,
            area: ['850px', '660px'],
            maxmin: true,
            content: sf.contentPath() + 'log.html?id=' + $(this).data('id')
        });
        parent.layer.full(layer_index);
	}).on('click','.btn-edit-cron',function(){
		var id = $(this).data('id');
		var value = $(this).data('cron') || '';
		parent.$table = $table;
		parent.layer.open({
			type: 2,
			area: ['850px', '660px'],
			fixed: false,
			maxmin: true,
			content: sf.contentPath() + 'editCron.html?id=' + id + '&cron=' + value
		});
	}).on('click','.btn-details',function () {
        var layer_index = parent.layer.open({
            type: 2,
            area: ['850px', '660px'],
            maxmin: true,
            content: sf.contentPath() + 'editor.html?id=' + $(this).data('id')
        });
        parent.layer.full(layer_index);
	}).on('click','.btn-notice',function () {
		var id = $(this).data('id');
		parent.$table = $table;
		parent.layer.open({
			type: 2,
			area: ['700px', '400px'],
			fixed: false,
			maxmin: true,
			content: sf.contentPath() + 'spiderList-notice.html?id=' + id
		});
	})
</script>
<script type="text/html" id="execute-count">
	<a class="layui-btn layui-btn-sm btn-task" data-name="{{d.name}}" data-id="{{d.id}}">{{d.running || 0}}/{{d.executeCount || 0}}</a>
</script>
<script type="text/html" id="buttons">
	<a class="layui-btn layui-btn-sm btn-details" data-id="{{d.id}}" data-name="{{d.name}}" title="查看"><i class="layui-icon">&#xe615;</i></a>
	<a class="layui-btn layui-btn-sm btn-notice" data-id="{{d.id}}" title="通知设置"><i class="layui-icon">&#xe667;</i></a>
	<a class="layui-btn layui-btn-sm btn-run" data-id="{{d.id}}" title="手动运行"><i class="layui-icon">&#xe623;</i></a>
	<a class="layui-btn layui-btn-sm btn-log" data-name="{{d.name}}" data-id="{{d.id}}" title="日志"><i class="layui-icon">&#xe60e;</i></a>
	<a class="layui-btn layui-btn-sm btn-remove" data-id="{{d.id}}" title="删除"><i class="layui-icon">&#xe640;</i></a>
</script>
</body>
</html>
